package com.ruoyi.organization.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.organization.domain.vo.CommunityScore;
import com.ruoyi.organization.domain.vo.CommunityScoreDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.organization.mapper.BizFamilyScoreLogMapper;
import com.ruoyi.organization.domain.BizFamilyScoreLog;
import com.ruoyi.organization.service.IBizFamilyScoreLogService;

/**
 * 家庭积分流水明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@Service
public class BizFamilyScoreLogServiceImpl implements IBizFamilyScoreLogService 
{
    @Autowired
    private BizFamilyScoreLogMapper bizFamilyScoreLogMapper;

    /**
     * 查询家庭积分流水明细
     * 
     * @param id 家庭积分流水明细主键
     * @return 家庭积分流水明细
     */
    @Override
    public BizFamilyScoreLog selectBizFamilyScoreLogById(Long id)
    {
        return bizFamilyScoreLogMapper.selectBizFamilyScoreLogById(id);
    }

    /**
     * 查询家庭积分流水明细列表
     * 
     * @param bizFamilyScoreLog 家庭积分流水明细
     * @return 家庭积分流水明细
     */
    @Override
    public List<BizFamilyScoreLog> selectBizFamilyScoreLogList(BizFamilyScoreLog bizFamilyScoreLog)
    {
        return bizFamilyScoreLogMapper.selectBizFamilyScoreLogList(bizFamilyScoreLog);
    }

    /**
     * 新增家庭积分流水明细
     * 
     * @param bizFamilyScoreLog 家庭积分流水明细
     * @return 结果
     */
    @Override
    public int insertBizFamilyScoreLog(BizFamilyScoreLog bizFamilyScoreLog)
    {
        bizFamilyScoreLog.setCreateTime(DateUtils.getNowDate());
        return bizFamilyScoreLogMapper.insertBizFamilyScoreLog(bizFamilyScoreLog);
    }

    /**
     * 修改家庭积分流水明细
     * 
     * @param bizFamilyScoreLog 家庭积分流水明细
     * @return 结果
     */
    @Override
    public int updateBizFamilyScoreLog(BizFamilyScoreLog bizFamilyScoreLog)
    {
        bizFamilyScoreLog.setUpdateTime(DateUtils.getNowDate());
        return bizFamilyScoreLogMapper.updateBizFamilyScoreLog(bizFamilyScoreLog);
    }

    /**
     * 批量删除家庭积分流水明细
     * 
     * @param ids 需要删除的家庭积分流水明细主键
     * @return 结果
     */
    @Override
    public int deleteBizFamilyScoreLogByIds(Long[] ids)
    {
        return bizFamilyScoreLogMapper.deleteBizFamilyScoreLogByIds(ids);
    }

    /**
     * 删除家庭积分流水明细信息
     * 
     * @param id 家庭积分流水明细主键
     * @return 结果
     */
    @Override
    public int deleteBizFamilyScoreLogById(Long id)
    {
        return bizFamilyScoreLogMapper.deleteBizFamilyScoreLogById(id);
    }

    /**
     * 已发放的积分总数
     *
     * @return
     */
    @Override
    public Long sendTotal(Integer type, LocalDateTime start, LocalDateTime end) {
        return bizFamilyScoreLogMapper.sendTotal(type,start,end);
    }
    /**
     * 积分统计
     *
     * @return
     */
    @Override
    @CommunityScope(alias = "a")
    public CommunityScore communityScore(CommunityScoreDto communityScoreDto) {
        CommunityScore communityScore = new CommunityScore();

        CommunityScoreDto dto = new CommunityScoreDto();
        if (!communityScoreDto.getParams().isEmpty()) {
            dto.setParams(communityScoreDto.getParams());
        }
        dto.setOrderScoreType(1);
//        //总计已发放
        Long alreadyScore = bizFamilyScoreLogMapper.communityScore(dto);
//        //总计已兑换积分
        dto.setOrderScoreType(2);
        Long consumeScore = bizFamilyScoreLogMapper.communityScore(dto);
        //今日已发放
        dto.setOrderScoreType(1);
        dto.setStart(DateUtils.start());
        dto.setEnd(DateUtils.end());
        Long todayScore = bizFamilyScoreLogMapper.communityScore(dto);
        //今日已兑换积分
        dto.setOrderScoreType(2);
        Long todayConsumeScore = bizFamilyScoreLogMapper.communityScore(dto);

        communityScore.setAlreadyScore(alreadyScore);
        communityScore.setConsumeScore(consumeScore);
        communityScore.setTodayScore(todayScore);
        communityScore.setTodayConsumeScore(todayConsumeScore);
        return communityScore;
    }
}
